﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;

namespace EEG_NIRS_Record
{
    public partial class NIRSForm : Form
    {
        MainForm _mainform;
        ZedGraphControl zed;
        int num_of_nirs;

        public NIRSForm(MainForm mainform)
        {            
            InitializeComponent();

            _mainform = mainform;

            num_of_nirs = _mainform.configuration.nirs_list.Count;
            Init_NIRS_Layout(tableLayoutPanel, num_of_nirs);
        }

        private void NIRSForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _mainform.where_nirs = 0;
            _mainform.RecoverTabPage(1);
        }
        private void Init_NIRS_Layout(TableLayoutPanel tableLayoutPanel, int ch)
        {
            SetRowColumn(tableLayoutPanel, ch);
            tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

            for (int i = 0; i < tableLayoutPanel.RowCount; i++)
            {
                tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            }
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
            {
                tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            }

            LineItem lineitem;
            RollingPointPairList list1_hbo = new RollingPointPairList(300);
            RollingPointPairList list1_hb = new RollingPointPairList(300);
            Color red = Color.Red;
            Color green = Color.Green;
            int ii = 0;
            int jj = 0;
            for (int i = 0; i < ch; i++)
            {
                zed = new ZedGraphControl();
                zed.Dock = DockStyle.Fill;
                zed.Margin = new Padding(0);
                GraphPane myPane = zed.GraphPane;
                myPane.Border.IsVisible = false;
                myPane.Legend.FontSpec.Size = 10F;
                myPane.Title.Text = "CH" + (i + 1).ToString();
                myPane.XAxis.Title.Text = "";
                myPane.YAxis.Title.Text = "HR";
                myPane.XAxis.Type = AxisType.Date;
                myPane.XAxis.Scale.Format = "HH:mm:ss";
                myPane.YAxis.Title.FontSpec.Size = 10F;
                myPane.XAxis.Scale.FontSpec.Size = 10F;
                myPane.YAxis.Scale.FontSpec.Size = 10F;
                //myPane.YAxis.Scale.Max = 0.5;
                //myPane.YAxis.Scale.Min = -0.5;
                myPane.XAxis.MajorGrid.IsVisible = true;
                myPane.YAxis.MajorGrid.IsVisible = true;
                myPane.XAxis.MajorGrid.Color = Color.Gray;
                myPane.YAxis.MajorGrid.Color = Color.Gray;
                string str = "HbO";
                lineitem = myPane.AddCurve(str, list1_hbo, red, SymbolType.None);
                lineitem.Line.Width = 2.0F;
                str = "Hb";
                lineitem = myPane.AddCurve(str, list1_hb, green, SymbolType.None);
                lineitem.Line.Width = 2.0F;

                tableLayoutPanel.Controls.Add(zed, jj, ii);
                jj++;  // 控制zed分布
                if (jj == tableLayoutPanel.ColumnCount)
                {
                    jj = 0;
                    ii++;
                }
            }
        }
        private void SetRowColumn(TableLayoutPanel tableLayoutPanel, int row, int column)
        {
            tableLayoutPanel.Controls.Clear();
            tableLayoutPanel.RowCount = row;
            tableLayoutPanel.ColumnCount = column;
        }
        private void SetRowColumn(TableLayoutPanel tableLayoutPanel,int ch )
        {
            if (ch == 1)
            {
                SetRowColumn(tableLayoutPanel, 1, 1);
            }
            else if (ch == 2)
            {
                SetRowColumn(tableLayoutPanel, 2, 1);
            }
            else if (ch <= 4)
            {
                SetRowColumn(tableLayoutPanel, 2, 2);
            }
            else if (ch <= 6)
            {
                SetRowColumn(tableLayoutPanel, 3, 2);
            }
            else if (ch <= 9)
            {
                SetRowColumn(tableLayoutPanel, 3, 3);
            }
            else if (ch <= 12)
            {
                SetRowColumn(tableLayoutPanel, 3, 4);
            }
            else if (ch <= 16)
            {
                SetRowColumn(tableLayoutPanel, 4, 4);
            }
            else if (ch <= 20)
            {
                SetRowColumn(tableLayoutPanel, 4, 5);
            }
            else if (ch <= 25)
            {
                SetRowColumn(tableLayoutPanel, 5, 5);
            }
            else if (ch <= 30)
            {
                SetRowColumn(tableLayoutPanel, 5, 6);
            }
            else if (ch <= 36)
            {
                SetRowColumn(tableLayoutPanel, 6, 6);
            }
            else if (ch <= 42)
            {
                SetRowColumn(tableLayoutPanel, 6, 7);
            }
            else if (ch <= 49)
            {
                SetRowColumn(tableLayoutPanel, 7, 7);
            }
            else
            {
                SetRowColumn(tableLayoutPanel, 8, 8);
            }
        }
        public void DrawNIRS(Hbr[] hbr)
        {
            DateTime currentTime = DateTime.Now;
            double time = (double)new XDate(currentTime);
            int i = 0;
            foreach (ZedGraphControl zed in tableLayoutPanel.Controls)
            {
                IPointListEdit list1 = zed.GraphPane.CurveList[0].Points as IPointListEdit;
                list1.Add(time, hbr[i].hbo);
                list1 = zed.GraphPane.CurveList[1].Points as IPointListEdit;
                list1.Add(time, hbr[i].hb);
                i++;
                this.BeginInvoke(new MethodInvoker(delegate { zed.AxisChange(); zed.Refresh(); }));
            }
        }
    }
}
